<template>
    <div class="count-box">
        <button class="minus" @click="minusCount">-</button>
        <input :value="value" @change="handleCountChange" class="inp" type="text">
        <button class="plus" @click="plusCount">+</button>
    </div>
</template>

<script>
export default {
  name: 'CountBox',
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    minusCount () {
      if (this.value <= 1) {
        // 数量不能小于1
        return
      }
      this.$emit('input', this.value - 1)
    },
    plusCount () {
      this.$emit('input', this.value + 1)
    },
    handleCountChange (e) {
    //   console.log(e.target.value)
      const count = +e.target.value
      if (isNaN(count) || count < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', count)
    }
  }
}
</script>

<style lang="less" scoped>
.count-box {
    display: flex;
    width: 110px;
    .minus,.plus {
        width: 30px;
        height: 30px;
        border: none;
        outline: none;
        background-color: #efefef;
    }
    .inp {
        width: 40px;
        height: 30px;
        margin: 0 5px;
        border: none;
        outline: none;
        background-color: #efefef;
        text-align: center;
    }
}
</style>
